﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;

namespace SharpSoft.Web
{
    //public class AuthorizationFilter : IAsyncAuthorizationFilter
    //{ 

    //    public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
    //    {
    //        if (context.ActionDescriptor is Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor cad)
    //        {
    //            if (cad.MethodInfo.ReturnType == typeof(ApiResult) || cad.MethodInfo.ReturnType == typeof(Task<ApiResult>))
    //            {
    //                bool needauth = true;
    //                foreach (var item in context.Filters)
    //                {
    //                    if (item is Microsoft.AspNetCore.Mvc.Authorization.AllowAnonymousFilter)
    //                    {
    //                        needauth = false;
    //                        break;
    //                    }
    //                }
    //                if (needauth)
    //                {
    //                    //bool isauth = context.HttpContext.User.Identity.IsAuthenticated;
    //                    //if (!isauth)
    //                    //{
    //                    //    context.Result =  new JsonResult(new ApiResult(-401, "用户未登录或登录已失效。"));
    //                    //}
    //                    //else
    //                    //{
    //                    //    var result = await context.HttpContext.AuthenticateAsync();
    //                    //    if (!result.Succeeded)
    //                    //    {//权限认证不成功
    //                    //        context.Result = new JsonResult(new ApiResult(-403, "您无权进行此操作。"));
    //                    //    }
    //                    //}
    //                }
    //            }
    //        }
    //    }
    //}
}
